package com.yxzr.youyou.fragment.vip;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.R;
import com.yxzr.youyou.adapter.MovieRecyclerViewAdapter;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.HomeBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class VipHomePageFragment extends BaseFragment {
    private RecyclerView movieRV,tvplayRV,comicRV,varietyRV;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_homepage,null);
        initView(v);
        return v;
    }

    private void initView(View v) {
        movieRV = v.findViewById(R.id.movie_rv);
        tvplayRV = v.findViewById(R.id.tvplay_rv);
        comicRV = v.findViewById(R.id.comic_rv);
        varietyRV = v.findViewById(R.id.variety_rv);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),3);
        movieRV.setLayoutManager(layoutManager);
        movieRV.addItemDecoration(new RecyclerItemDecoration(10,3));
        movieRV.setNestedScrollingEnabled(false);


        GridLayoutManager layoutManager1 = new GridLayoutManager(getContext(),3);
        tvplayRV.setLayoutManager(layoutManager1);
        tvplayRV.addItemDecoration(new RecyclerItemDecoration(10,3));
        tvplayRV.setNestedScrollingEnabled(false);


        GridLayoutManager layoutManager2 = new GridLayoutManager(getContext(),3);
        comicRV.setLayoutManager(layoutManager2);
        comicRV.addItemDecoration(new RecyclerItemDecoration(10,3));
        comicRV.setNestedScrollingEnabled(false);


        GridLayoutManager layoutManager3 = new GridLayoutManager(getContext(),3);
        varietyRV.setLayoutManager(layoutManager3);
        varietyRV.addItemDecoration(new RecyclerItemDecoration(10,3));
        varietyRV.setNestedScrollingEnabled(false);

        initData();
    }

    private void initData(){
        RequestUtil.getInstence().API().homeTitle("1")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        HomeBean bean = gson.fromJson(t,HomeBean.class);
                        if (bean.getData() != null && bean.getData().size() > 4){
                            if (bean.getData().get(0).getData() != null && bean.getData().get(0).getData().size() > 0){
                                tvplayRV.setAdapter(new MovieRecyclerViewAdapter(getContext(),bean.getData().get(0).getData(),1));
                            }
                            if (bean.getData().get(1).getData() != null && bean.getData().get(1).getData().size() > 0){
                                movieRV.setAdapter(new MovieRecyclerViewAdapter(getContext(),bean.getData().get(1).getData(),1));
                            }
                            if (bean.getData().get(2).getData() != null && bean.getData().get(2).getData().size() > 0){
                                comicRV.setAdapter(new MovieRecyclerViewAdapter(getContext(),bean.getData().get(2).getData(),1));
                            }
                            if (bean.getData().get(3).getData() != null && bean.getData().get(3).getData().size() > 0){
                                varietyRV.setAdapter(new MovieRecyclerViewAdapter(getContext(),bean.getData().get(3).getData(),1));
                            }
                        }

                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
